/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import BUS.GaBUS;
import BUS.VeBUS;
import DAO.ExcuteQuery;
import DTO.ChuyenTauDTO;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PC
 */
public class ThongKeBanVe extends javax.swing.JPanel {

    /**
     * Creates new form ThongKeBanVe
     */
    public ThongKeBanVe() {
        initComponents();
        TenChuyen();
        load();
        Tuan();
        Thang();
        Nam();
        Quy1();
        Quy2();
        Quy3();
        Quy4();
    }
     Calendar cal = Calendar.getInstance();
    public void Tuan()
    {
        
          int Ngay=cal.get(Calendar.DATE);
        int Thang=cal.get(Calendar.MONTH) + 1;
        int Nam=cal.get(Calendar.YEAR);
        Ngay=Ngay-7;
        if(Ngay<1)
        {
            Ngay=30-Ngay;
            Thang=Thang-1;
            if(Thang<1)
            {
                Thang=12-Thang;
                Nam=Nam-1;
            }
        }
      String Tuan=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDen.addItem(Tuan);

    }
      public void Thang()
    {
         int Ngay=cal.get(Calendar.DATE);
        int Thang=cal.get(Calendar.MONTH) + 1;
        int Nam=cal.get(Calendar.YEAR);
       Thang=Thang+1;
        if(Ngay>30)
        {
            Ngay=Ngay-30;
            Thang=Thang+1;
            if(Thang>12)
            {
                Thang=Thang-12;
                Nam=Nam+1;
            }
        }
      String Tuan=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDen.addItem(Tuan);

    }
        public void Nam()
    {
         int Ngay=cal.get(Calendar.DATE);
        int Thang=cal.get(Calendar.MONTH) + 1;
        int Nam=cal.get(Calendar.YEAR);
        Nam=Nam+1;
        if(Ngay>30)
        {
            Ngay=Ngay-30;
            Thang=Thang+1;
            if(Thang>12)
            {
                Thang=Thang-12;
                Nam=Nam+1;
            }
        }
      String Tuan=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDen.addItem(Tuan);

    }
      public void Quy1()
    {
         int Ngay=1;
        int Thang=1;
        int Nam=cal.get(Calendar.YEAR);
        
      String Di=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDi.addItem(Di);
           Thang=Thang+3;
            String Den=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
            jComboBoxNgayDen.addItem(Den);
    }
         public void Quy2()
    {
         int Ngay=1;
        int Thang=4;
        int Nam=cal.get(Calendar.YEAR);
        
      String Di=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDi.addItem(Di);
           Thang=Thang+3;
            String Den=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
            jComboBoxNgayDen.addItem(Den);
    }
              public void Quy3()
    {
         int Ngay=1;
        int Thang=7;
        int Nam=cal.get(Calendar.YEAR);
        
      String Di=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDi.addItem(Di);
           Thang=Thang+3;
            String Den=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
            jComboBoxNgayDen.addItem(Den);
    }
                       public void Quy4()
    {
         int Ngay=1;
        int Thang=10;
        int Nam=cal.get(Calendar.YEAR);
        
      String Di=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
           jComboBoxNgayDi.addItem(Di);
           Thang=Thang+3;
           if(Thang>12)
           {
               Thang=Thang-12;
               Nam=Nam+1;
           }
            String Den=String.valueOf(Nam)+"-"+String.valueOf(Thang)+"-"+String.valueOf(Ngay);
            jComboBoxNgayDen.addItem(Den);
    }
    public  void load()
    {
        
        
        String NgayHT =String.valueOf(cal.get(Calendar.YEAR)) + "-" + String.valueOf(cal.get(Calendar.MONTH) + 1) + "-" +String.valueOf(cal.get(Calendar.DATE));
  
        
         jComboBoxNgayDi.addItem(NgayHT);
          jComboBoxNgayDen.addItem(NgayHT);
         jComboBoxTuyChon.addItem("Hôm Nay");
          jComboBoxTuyChon.addItem("Tuần Này");
           jComboBoxTuyChon.addItem("Tháng Này");
          jComboBoxTuyChon.addItem("Năm Nay");
          jComboBoxTuyChon.addItem("Quý 1");
          jComboBoxTuyChon.addItem("Quý 2");
          jComboBoxTuyChon.addItem("Quý 3");
          jComboBoxTuyChon.addItem("Quý 4");
    }
public void TenChuyen()
    {
      
        try {
            String sql = "Select * from chuyen";
            ResultSet rs = ExcuteQuery.TimKiemTatCa(sql);
            ArrayList<ChuyenTauDTO> dslnvl = new ArrayList<ChuyenTauDTO>();
            while (rs.next()) {
                ChuyenTauDTO temp = new ChuyenTauDTO();
                temp.id= rs.getString("id");
          
                dslnvl.add(temp);
            }
            for (ChuyenTauDTO k : dslnvl) {
                a.addElement(k.id);
            }
            jComboBoxChuyen.setModel(a);
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
    }
DefaultComboBoxModel<String> a = new DefaultComboBoxModel<String>();
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBoxTuyChon = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxNgayDi = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxNgayDen = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxChuyen = new javax.swing.JComboBox();
        jButtonXem = new javax.swing.JButton();
        jButtonIn = new javax.swing.JButton();
        jButtonInXuat = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 255, 255)));

        jLabel1.setText("Tùy Chọn :");

        jComboBoxTuyChon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTuyChonActionPerformed(evt);
            }
        });

        jLabel2.setText("Ngày Đi :");

        jComboBoxNgayDi.setForeground(new java.awt.Color(255, 0, 0));
        jComboBoxNgayDi.setEnabled(false);

        jLabel3.setText("Ngày Đến :");

        jComboBoxNgayDen.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxTuyChon, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxNgayDi, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxNgayDen, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxTuyChon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxNgayDi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxNgayDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 255, 255)));

        jLabel4.setText("Chuyến Tàu :");

        jComboBoxChuyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButtonXem.setText("Xem");
        jButtonXem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXemActionPerformed(evt);
            }
        });

        jButtonIn.setText("In");

        jButtonInXuat.setText("Xuất File");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78)
                .addComponent(jButtonXem, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jButtonIn, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jButtonInXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jComboBoxChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonXem, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                        .addComponent(jButtonIn, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                        .addComponent(jButtonInXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxTuyChonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTuyChonActionPerformed
        if(jComboBoxTuyChon.getSelectedItem()=="Hôm Nay")
        {
         jComboBoxNgayDen.setSelectedIndex(0);
         jComboBoxNgayDi.setSelectedIndex(0);
        }
          if(jComboBoxTuyChon.getSelectedItem()=="Tuần Này")
        {
         jComboBoxNgayDen.setSelectedIndex(1);
         jComboBoxNgayDi.setSelectedIndex(0);
        }
           if(jComboBoxTuyChon.getSelectedItem()=="Tháng Này")
        {
         jComboBoxNgayDen.setSelectedIndex(2);
         jComboBoxNgayDi.setSelectedIndex(0);
        }
            if(jComboBoxTuyChon.getSelectedItem()=="Năm Nay")
        {
         jComboBoxNgayDen.setSelectedIndex(3);
         jComboBoxNgayDi.setSelectedIndex(0);
        }
             if(jComboBoxTuyChon.getSelectedItem()=="Quý 1")
        {
         jComboBoxNgayDen.setSelectedIndex(4);
         jComboBoxNgayDi.setSelectedIndex(1);
        }
             if(jComboBoxTuyChon.getSelectedItem()=="Quý 2")
        {
         jComboBoxNgayDen.setSelectedIndex(5);
         jComboBoxNgayDi.setSelectedIndex(2);
        }
         if(jComboBoxTuyChon.getSelectedItem()=="Quý 3")
        {
         jComboBoxNgayDen.setSelectedIndex(6);
         jComboBoxNgayDi.setSelectedIndex(3);
        }
           if(jComboBoxTuyChon.getSelectedItem()=="Quý 4")
        {
         jComboBoxNgayDen.setSelectedIndex(7);
         jComboBoxNgayDi.setSelectedIndex(4);
        }
    }//GEN-LAST:event_jComboBoxTuyChonActionPerformed

    private void jButtonXemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXemActionPerformed
        try {
     
            String ngaydi=jComboBoxNgayDi.getSelectedItem().toString();
            String ngayden=jComboBoxNgayDen.getSelectedItem().toString();
            String chuyen=jComboBoxChuyen.getSelectedItem().toString();
            ResultSet rs = VeBUS.BaoCao(ngaydi,ngayden,chuyen);
            Vector<String> colName = new Vector<>();
            ResultSetMetaData data = rs.getMetaData();
            int count = data.getColumnCount();
            colName.add("Mã Vé");
            colName.add("Tình Trạng Vé");
            colName.add("Ghế");
            colName.add("Giá Vé");
            colName.add("Chuyến");
            colName.add("Hóa Đơn");
            colName.add("Ngày Tạo");
            Vector row;
            DefaultTableModel dtm = new DefaultTableModel() {
                @Override
                public boolean isCellEditable(int i, int i1) {
                    return false;
                }
            };
            dtm.setColumnIdentifiers(colName);

            while (rs.next()) {
                row = new Vector();
                for (int i = 1; i <= count; i++) {
                    row.add(rs.getString(i));
                }
                dtm.addRow(row);
            }

            jTable1.setModel(dtm);
             } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }//GEN-LAST:event_jButtonXemActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonIn;
    private javax.swing.JButton jButtonInXuat;
    private javax.swing.JButton jButtonXem;
    private javax.swing.JComboBox jComboBoxChuyen;
    private javax.swing.JComboBox jComboBoxNgayDen;
    private javax.swing.JComboBox jComboBoxNgayDi;
    private javax.swing.JComboBox jComboBoxTuyChon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
